﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InversPong.Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using InversPong.Utilities;

namespace InversPong.Views
{
    public class PlayerInfoView : DrawableGameComponent, GameView
    {
        
        private Player player;
        public Vector2 Origin { get; set; }

        private SpriteBatch spriteBatch;
        private SpriteFont spriteFontMain;
        private Texture2D baseTexture;
        private Texture2D gameoverTexture;
        private Texture2D lifeTexture;
        private Color PlayerColor;

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            spriteFontMain = Game.Content.Load<SpriteFont>("playerInfoFont");

            baseTexture = Game.Content.Load<Texture2D>("gfx\\infoPlayer");
            lifeTexture = Game.Content.Load<Texture2D>("gfx\\coeur");

        }

        public override void Draw(GameTime gameTime)
        {
            Texture2D texture = baseTexture;
            spriteBatch.Begin();
            spriteBatch.Draw(baseTexture, Origin, Color.White);

            Vector2 lifePosition = new Vector2(7 + (int)Origin.X, 28 + (int)Origin.Y);
            Vector2 bonusPosition = new Vector2(228 + (int)Origin.X, 25 + (int)Origin.Y);
            Vector2 timeBonusPostion = new Vector2(250 + (int)Origin.X, 75 + (int)Origin.Y);
            

            if (player.Name == "PLAYER 1")
            {
                PlayerColor = Color.DarkBlue;
            }
            else
            {
                PlayerColor = Color.Yellow;
            }


            //ajouter autant de coeur que de point de vie 
            for (int i = 0; i < player.Lives; i++) {
                spriteBatch.Draw(lifeTexture, lifePosition, Color.White);
                lifePosition.X += lifeTexture.Width;
            }

            //ajouter les bonus en cours -
            IList<PowerUp> currentBonus = player.Bat.GetPowerUps();
                
            foreach (PowerUp i in currentBonus)
            {
                Services.ISpriteDrawer spriteDrawer = (Services.ISpriteDrawer)Game.Services.GetService(typeof(Services.ISpriteDrawer));
                Drawing.DrawCenteredText(spriteFontMain, spriteBatch, i.Timer.Seconds.ToString(), timeBonusPostion, PlayerColor, false);
                spriteDrawer.DrawObject(spriteBatch, i, Color.White, bonusPosition);
                bonusPosition.X += 50;
                timeBonusPostion.X += 50;
            }
               
            //écrire le nom du jouer
            Vector2 ori = Origin;
            ori.X += 442;
            ori.Y += 53;
            Drawing.DrawCenteredText(spriteFontMain,spriteBatch, player.Name, ori, PlayerColor, false);
            spriteBatch.End();
        }

        public PlayerInfoView(Microsoft.Xna.Framework.Game game, Player player, Vector2 origin)
            : base(game)
        {
            if (player != null)
            {
                this.player = player;
                Origin = origin;
                DrawOrder = Order.StaticLevel;
                game.Components.Add(this);
            }
            else
            {
                throw new ArgumentNullException("player");
            }
        }
    }
}
